package com.linln.modules.system.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linln.common.utils.UUIDGenerator;
import com.linln.modules.system.dao.ActivityExpandMapper;
import com.linln.modules.system.dao.CustomMapper;
import com.linln.modules.system.dao.UserExpandMapper;
import com.linln.modules.system.entity.ActivityExpand;
import com.linln.modules.system.entity.ActivityExpandExample;
import com.linln.modules.system.vo.ExpandMap;
import com.linln.modules.system.vo.UserExpandNumVo;
import com.linln.modules.system.vo.UserExpandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ShareServices {

    @Autowired
    private ActivityExpandMapper activityExpandMapper;

    @Autowired
    private UserExpandMapper expandMapper;

    @Autowired
    private CustomMapper customMapper;


    public Page<ActivityExpand> getUserPager(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<ActivityExpand> users = customMapper.getActivityExpand();
        return users;
    }


    public int saveShare(String remark, String varpath, String params) {
        ActivityExpandExample example = new ActivityExpandExample();
        ActivityExpandExample.Criteria to = example.createCriteria().andParamsEqualTo(params);
        int i = activityExpandMapper.countByExample(example);
        if (i > 0) {
            return 0;
        } else {
            ActivityExpand expand = new ActivityExpand();
            expand.setId(UUIDGenerator.generate());
            expand.setVarpath(varpath);
            expand.setParams(params);
            expand.setClicks(0);
            expand.setReally(0);
            expand.setCreatetime(new Date());
            expand.setRemark(remark);
            int insert = activityExpandMapper.insert(expand);
            return insert;
        }
    }


    public List<UserExpandVo> getActivityExpand(String aId, String startTime,Integer auth) {
        ActivityExpand expand = activityExpandMapper.selectByPrimaryKey(aId);
        String params = expand.getParams();
        List<UserExpandVo> vos = customMapper.getExpandUserVo(params, startTime,auth);
        return vos;
    }

    public ExpandMap getExpandMap(String aId) {
        ActivityExpand expand = activityExpandMapper.selectByPrimaryKey(aId);
        String params = expand.getParams();
        List<UserExpandVo> vos = customMapper.getExpandUserVo(params, null,null);
        List<UserExpandNumVo> vo = customMapper.getExpandUserNumVo(aId, null, null);
        ExpandMap map = new ExpandMap(vos, vo);
        return map;

    }
}
